﻿Shader "Unlit/Half lambert"
{
    Properties
    {
       /* _MainTex ("Texture", 2D) = "white" {}*/
        _Diffuse("Diffuse",Color)=(1,1,1,1)
    }
    SubShader
    {
       /* Tags { "RenderType"="Opaque" }
        LOD 100*/

        Pass
        {
            Tags{"LightMode"="ForwardBase"}
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            //#pragma multi_compile_fog

            #include"UnityCG.cginc"
            #include"Lighting.cginc"

            fixed4 _Diffuse;

            struct appdata
            {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
                //float4 vertex : POSITION;
                //float2 uv : TEXCOORD0;
            };

            //与逐顶点不同的顶点着色器的输出结构体
            struct v2f
             {
                float4 pos : SV_POSITION;
                 float3 worldNormal : TEXCOORD0;
            };

          //顶点着色器不需要计算光照模型，只需要把世界空间下的法线传递给片元着色器即可：
            v2f vert(appdata v) {
               v2f o;
             //Transform the vertex from object space to projection space
             o.pos = UnityObjectToClipPos(v.vertex);

             //Transform the normal fram object space to world space
             o.worldNormal = mul(v.normal, (float3x3)unity_WorldToObject);

             return o;
            }
            //只需要修改片元着色器中计算漫反射光照部分      
            fixed4 frag(v2f i) : SV_Target 
            {
                //Get ambient term
            fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;

            fixed3 worldNormal = normalize(i.worldNormal);
            //Get the light direnction in world space
            fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);

            //Compute diffuse term
            fixed halfLambert = dot(worldNormal, worldLightDir) * 0.5 + 0.5;
            fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * halfLambert;

            fixed3 color = ambient + diffuse;

            return fixed4(color, 1.0);

            }
            ENDCG
        }
    }
}
